package com.gitee.yannzh.rune.support.fs.converter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gitee.yannzh.rune.support.crud.ViewContext;
import com.gitee.yannzh.rune.support.crud.ViewResolver;
import com.gitee.yannzh.rune.support.crud.constants.ViewType;
import com.gitee.yannzh.rune.support.crud.model.ViewFieldModel;
import com.gitee.yannzh.rune.support.crud.model.ViewModel;
import com.gitee.yannzh.rune.support.fs.constants.ColumnType;
import com.gitee.yannzh.rune.support.fs.model.Column;
import com.gitee.yannzh.rune.support.fs.model.Columns;
import com.gitee.yannzh.rune.support.fs.model.FastCrudOptions;
import lombok.SneakyThrows;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author Yann
 * @date 2025/4/21 13:34
 * @description
 * @since 1.0.0
 */
public class FsConverter {
    static ObjectMapper mapper = new ObjectMapper();

    @SneakyThrows
    public static void main(String[] args) {
        ViewResolver.resolve("com.gitee.yannzh.rune.support.fs.test");

        FastCrudOptions options = convert("user");
        System.out.println(mapper.writeValueAsString(options));
    }

    public static FastCrudOptions convert(String viewName) {
        ViewModel viewModel = ViewContext.getViewModel(viewName);
        FastCrudOptions fastCrudOptions = new FastCrudOptions();
        List<ViewFieldModel> fields = viewModel.getColumns();
        Columns columns = fields.stream().collect(Collectors.toMap(ViewFieldModel::getFieldName, Column::CreateFsColumn, (existing, replacement) -> existing, Columns::new));
        fastCrudOptions.setColumns(columns);
        return fastCrudOptions;
    }

    public static String convertType(ViewType type) {
        switch (type) {
            // -----------------    基本类型     -----------------------
            case TEXT:
                return ColumnType.TEXT.getValue();
            case TEXTAREA:
                return ColumnType.TEXTAREA.getValue();
            case PASSWORD:
                return ColumnType.PASSWORD.getValue();
            case NUMBER:
                return ColumnType.NUMBER.getValue();
            case PHONE:
                return ColumnType.PHONE.getValue();

            // -----------------    日期时间类型     -----------------------
            case DATE:
                return ColumnType.DATE.getValue();
            case DATETIME:
                return ColumnType.DATETIME.getValue();
            case TIME:
                return ColumnType.TIME.getValue();
            case DATE_RANGE:
                return ColumnType.DATE_RANGE.getValue();
            case DATE_TIME_RANGE:
                return ColumnType.DATE_TIME_RANGE.getValue();

            // -----------------    字典类型     -----------------------
            case DICT_RADIO:
                return ColumnType.DICT_RADIO.getValue();
            case DICT_CHECKBOX:
                return ColumnType.DICT_CHECKBOX.getValue();
            case DICT_SELECT:
                return ColumnType.DICT_SELECT.getValue();
            case DICT_SWITCH:
                return ColumnType.DICT_SWITCH.getValue();
            case DICT_CASCADER:
                return ColumnType.DICT_CASCADER.getValue();
            default:
                return ColumnType.TEXT.getValue();
        }
    }

}
